package com.lysj.food.food.controller;

import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseController;
import com.lysj.admin.utils.spring.foundation.pagination.PageUtil;
import com.lysj.admin.utils.spring.foundation.pagination.PageVo;
import com.lysj.admin.utils.web.Resp;
import com.lysj.food.food.domain.Food;
import com.lysj.food.food.domain.FoodGuiGe;
import com.lysj.food.food.dto.OrderAddDto;
import com.lysj.food.food.service.FoodAttributeDetailService;
import com.lysj.food.food.service.FoodGuigeService;
import com.lysj.food.food.service.FoodService;
import com.lysj.food.food.vo.BackGroundFoodVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Created by wtl on 2019-03-19 16:00:45
 * @description 菜品控制层
 */
@Slf4j
@RestController
@RequestMapping("/food/food")
public class FoodController extends BaseController<Food> {

    @Resource
    private FoodService foodService;

    @Override
    public FoodService getService() {
        return foodService;
    }

    @Resource
    private FoodGuigeService foodGuigeService;

    @Resource
    private FoodAttributeDetailService foodAttributeDetailService;


    /**
     * @author Created by wtl on 2019/3/27 15:04
     * @Description 获取本店还未默认下单的菜品，已设置为默认下单的菜品不显示
     */
    @GetMapping("/find_not_default_food")
    public Resp findNotDefaultFood(String storeId, PageVo pageVo) {
        return new Resp().success(foodService.findNotDefaultFood(storeId, pageVo));
    }

    /**
     * @author Created by wtl on 2019/4/1 15:12
     * @Description 点菜下单接口
     */
    @PostMapping("/create_order")
    public Resp createOrder(OrderAddDto orderAddDto) {
        return new Resp().success(foodService.createOrder(orderAddDto), "下单成功");
    }

    /*
     * @author drj
     * @date 2019-04-02 18:44
     * @Description :获取本门店所有菜品(类型名称,菜品id,菜品名称，销售价)
     */
    @GetMapping("/list_rewrite")
    public Resp createOrder(String storeId, PageVo pageVo) {
        Map<String, String> map = foodAttributeDetailService.map(storeId);

        Pageable pageable = PageUtil.initPage(pageVo);
        Page<BackGroundFoodVo> page = foodService.getRepository().findStoreFoodList(pageable, storeId);
        //获取规格信息
        for (BackGroundFoodVo backGroundFoodVo : page) {
            List<FoodGuiGe> foodGuiGeList = foodGuigeService.getRepository().findByFoodIdAndDelFlag(backGroundFoodVo.getFoodId(), CommonConstant.NORMAL_FLAG);
            for (FoodGuiGe foodGuiGe : foodGuiGeList) {
                foodGuiGe.setName(map.get(foodGuiGe.getFoodAttributeDetailId()));
            }
            backGroundFoodVo.setFoodGuiGeList(foodGuiGeList);
        }
        return new Resp().success(page);
    }

    /*
     * @author drj
     * @date 2019-04-16 10:16
     * @Description :查看单个菜品信息
     */
    @GetMapping("/find_one")
    public Resp findOne(String foodId) {
        return new Resp().success(foodService.findOne(foodId));
    }
}
